package bookstudytest.socket;

import java.io.*;
import java.net.Socket;

/**
 * @Auther: Lee
 * @Date: 2018/6/20 11:08
 * @Description:
 */
public class BufferClientSocket {
    public static void main(String[] args) {

        Socket socket=null;
        BufferedReader reader=null;
        PrintWriter writer=null;
        try {
            //客户端socket指定服务器的地址和端口号
            socket=new Socket("127.0.0.1", 56669);
            //获取服务端的输入流
            reader =new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //用于发送返回信息,
            writer =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
            for (int i = 0; i < 10; i++) {
                writer.println("body" + i);
                writer.flush();
                String str=reader.readLine();
                System.out.println(str);
            }

            writer.println("end");
            writer.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            try {
                reader.close();
                writer.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
